#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Linq;
using System.Web.Mvc;
using Jayrock.Json;
using Jayrock.Json.Conversion;
using raya.Library.Helpers;
using raya.Models.applications;
using raya.Models.people;

namespace raya.Controllers
{
    public class contactsController : baseController
    {
        //
        // GET: /contacts/
        public ActionResult Index(string userid)
        {
            if (Session["id"] == null)
            {
                return Redirect("/");
            }
            var people = new people();
            string id = (!string.IsNullOrEmpty(userid)) ? userid : Session["id"].ToString();
            var person = people.GetPerson(id, true);
            var friendsCount = people.GetFriendsCount(id);
            int page;
            if (Request.QueryString["page"] != null &&
                PHPExt.is_numeric(Request.QueryString["page"]) &&
                int.Parse(Request.QueryString["page"]) > 0 && int.Parse(Request.QueryString["page"]) <= friendsCount)
            {
                page = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                page = 1;
            }
            var start = (page - 1) * 8;
            var count = 8;
            var pages = int.Parse(Math.Ceiling(friendsCount / 8D).ToString());
            
            var friends = people.GetFriends(id, start, count);
            bool isFriend = Session["id"] != null ? Session["id"].ToString() == id ? true : people.IsFriend(id, Session["id"].ToString()) : false;
            Profile friendProfile = new Profile
                                        {
                                            is_friend = isFriend,
                                            page = page,
                                            pages = pages,
                                            friendsCount = friendsCount,
                                            friends = friends.ToList(),
                                            person = person,
                                            is_owner = (Session["id"] != null ? (Session["id"].ToString() == id) : false)
                                        };
            people.Dispose();
            return View("Index", friendProfile);
        }

        public ActionResult Add(string approved)
        {
            if (Session["id"] == null)
            {
                return Redirect("/login");
            }
            return View();   
        }
        public ActionResult Gmail()
        {
            if (Session["id"] == null)
            {
                return Redirect("/login");
            }

            var app = new applications();
            var g = app.get_application_by_url(Request.Url.GetLeftPart(UriPartial.Authority) +
                                               "/content/widgets/gmailContacts.xml");
            var profileApp = new ProfileApplication {app = g};
            gadgetInfo data = new gadgetInfo { gadget = profileApp, width = 676, personid = Session["id"].ToString(), view = "home" };

            // return success
            return View("widget", data);
        }

        public ActionResult Invite(string data)
        {
            JsonArray json = (JsonArray)JsonConvert.Import(data);
            foreach (string s in json)
            {
                var parts = s.Split(',');
                if (parts.Length != 2) 
                    continue;
                var name = parts[0];
                var email = parts[1];
            }
            // TODO: implement mail sending mechanism

            return Content("OK");
        }

        public ActionResult Search()
        {
            handleErrors("Not implemented");
            return Redirect("/");
        }

        public ActionResult List()
        {
            handleErrors("Not implemented");
            return Redirect("/");
        }

    }
}